#!/bin/bash

# This file is part of util-linux.
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

TS_TOPDIR="${0%/*}/../.."
TS_DESC="configs"

. "$TS_TOPDIR"/functions.sh
ts_init "$*"

[ "$("$TS_HELPER_SYSINFO" scandirat-ok)" = "1" ] || ts_skip "no scandirat"

ts_check_test_command "$TS_HELPER_CONFIGS"

# Create test directory structure
TESTDIR="$TS_OUTDIR/${TS_TESTNAME}-root"
ETC_DIR="$TESTDIR/etc"
RUN_DIR="$TESTDIR/run"
USR_DIR="$TESTDIR/usr"

mkdir -p "$ETC_DIR/proj"
mkdir -p "$RUN_DIR/proj"
mkdir -p "$USR_DIR/proj"

# Test 1: Main config file in /etc
ts_init_subtest "main-etc"
echo "main config" > "$ETC_DIR/proj/example.conf"
find "$TESTDIR" -type f | sed "s|$TESTDIR|@TESTDIR@|g" | sort >> $TS_OUTPUT
$TS_HELPER_CONFIGS --etc "$ETC_DIR" --run "$RUN_DIR" --usr "$USR_DIR" \
	--project "proj" --name "example" --suffix "conf" \
	| sed "s|$TESTDIR|@TESTDIR@|g" >> $TS_OUTPUT 2>> $TS_ERRLOG
rm -f "$ETC_DIR/proj/example.conf"
ts_finalize_subtest

# Test 2: Main config file in /usr (fallback)
ts_init_subtest "main-usr"
echo "usr config" > "$USR_DIR/proj/example.conf"
find "$TESTDIR" -type f | sed "s|$TESTDIR|@TESTDIR@|g" | sort >> $TS_OUTPUT
$TS_HELPER_CONFIGS --etc "$ETC_DIR" --run "$RUN_DIR" --usr "$USR_DIR" \
	--project "proj" --name "example" --suffix "conf" \
	| sed "s|$TESTDIR|@TESTDIR@|g" >> $TS_OUTPUT 2>> $TS_ERRLOG
rm -f "$USR_DIR/proj/example.conf"
ts_finalize_subtest

# Test 3: Drop-in files from /etc
ts_init_subtest "dropin-etc"
mkdir -p "$ETC_DIR/proj/example.conf.d"
echo "etc drop-in 1" > "$ETC_DIR/proj/example.conf.d/10-first.conf"
echo "etc drop-in 2" > "$ETC_DIR/proj/example.conf.d/20-second.conf"
find "$TESTDIR" -type f | sed "s|$TESTDIR|@TESTDIR@|g" | sort >> $TS_OUTPUT
$TS_HELPER_CONFIGS --etc "$ETC_DIR" --run "$RUN_DIR" --usr "$USR_DIR" \
	--project "proj" --name "example" --suffix "conf" \
	| sed "s|$TESTDIR|@TESTDIR@|g" >> $TS_OUTPUT 2>> $TS_ERRLOG
rm -rf "$ETC_DIR/proj/example.conf.d"
ts_finalize_subtest

# Test 4: Drop-in files from /usr
ts_init_subtest "dropin-usr"
mkdir -p "$USR_DIR/proj/example.conf.d"
echo "usr drop-in 1" > "$USR_DIR/proj/example.conf.d/10-first.conf"
echo "usr drop-in 2" > "$USR_DIR/proj/example.conf.d/20-second.conf"
find "$TESTDIR" -type f | sed "s|$TESTDIR|@TESTDIR@|g" | sort >> $TS_OUTPUT
$TS_HELPER_CONFIGS --etc "$ETC_DIR" --run "$RUN_DIR" --usr "$USR_DIR" \
	--project "proj" --name "example" --suffix "conf" \
	| sed "s|$TESTDIR|@TESTDIR@|g" >> $TS_OUTPUT 2>> $TS_ERRLOG
rm -rf "$USR_DIR/proj/example.conf.d"
ts_finalize_subtest

# Test 5: Main config + drop-ins from multiple directories (priority order)
ts_init_subtest "combined"
echo "main config" > "$ETC_DIR/proj/example.conf"
mkdir -p "$ETC_DIR/proj/example.conf.d"
mkdir -p "$RUN_DIR/proj/example.conf.d"
mkdir -p "$USR_DIR/proj/example.conf.d"
echo "usr drop-in" > "$USR_DIR/proj/example.conf.d/10-base.conf"
echo "run drop-in" > "$RUN_DIR/proj/example.conf.d/20-override.conf"
echo "etc drop-in" > "$ETC_DIR/proj/example.conf.d/30-local.conf"
find "$TESTDIR" -type f | sed "s|$TESTDIR|@TESTDIR@|g" | sort >> $TS_OUTPUT
$TS_HELPER_CONFIGS --etc "$ETC_DIR" --run "$RUN_DIR" --usr "$USR_DIR" \
	--project "proj" --name "example" --suffix "conf" \
	| sed "s|$TESTDIR|@TESTDIR@|g" >> $TS_OUTPUT 2>> $TS_ERRLOG
rm -f "$ETC_DIR/proj/example.conf"
rm -rf "$ETC_DIR/proj/example.conf.d"
rm -rf "$RUN_DIR/proj/example.conf.d"
rm -rf "$USR_DIR/proj/example.conf.d"
ts_finalize_subtest

# Test 6: Same basename in multiple directories (masking behavior)
ts_init_subtest "masking"
mkdir -p "$ETC_DIR/proj/example.conf.d"
mkdir -p "$USR_DIR/proj/example.conf.d"
echo "usr version" > "$USR_DIR/proj/example.conf.d/10-config.conf"
echo "etc version" > "$ETC_DIR/proj/example.conf.d/10-config.conf"
find "$TESTDIR" -type f | sed "s|$TESTDIR|@TESTDIR@|g" | sort >> $TS_OUTPUT
$TS_HELPER_CONFIGS --etc "$ETC_DIR" --run "$RUN_DIR" --usr "$USR_DIR" \
	--project "proj" --name "example" --suffix "conf" \
	| sed "s|$TESTDIR|@TESTDIR@|g" >> $TS_OUTPUT 2>> $TS_ERRLOG
rm -rf "$ETC_DIR/proj/example.conf.d"
rm -rf "$USR_DIR/proj/example.conf.d"
ts_finalize_subtest

# Test 7: No project subdirectory
ts_init_subtest "no-project"
mkdir -p "$ETC_DIR"
echo "root config" > "$ETC_DIR/test.conf"
find "$TESTDIR" -type f | sed "s|$TESTDIR|@TESTDIR@|g" | sort >> $TS_OUTPUT
$TS_HELPER_CONFIGS --etc "$ETC_DIR" --run "$RUN_DIR" --usr "$USR_DIR" \
	--name "test" --suffix "conf" \
	| sed "s|$TESTDIR|@TESTDIR@|g" >> $TS_OUTPUT 2>> $TS_ERRLOG
rm -f "$ETC_DIR/test.conf"
ts_finalize_subtest

# Test 8: Alphabetical sorting order with main config and masking
ts_init_subtest "sorting"
echo "main config" > "$ETC_DIR/proj/example.conf"
mkdir -p "$ETC_DIR/proj/example.conf.d"
mkdir -p "$USR_DIR/proj/example.conf.d"
# Create files with names that test sorting and masking
echo "aaa from usr" > "$USR_DIR/proj/example.conf.d/aaa.conf"
echo "zzz from usr" > "$USR_DIR/proj/example.conf.d/zzz.conf"
echo "mmm from usr" > "$USR_DIR/proj/example.conf.d/mmm.conf"
echo "bbb from etc" > "$ETC_DIR/proj/example.conf.d/bbb.conf"
echo "mmm from etc" > "$ETC_DIR/proj/example.conf.d/mmm.conf"  # masks usr/mmm.conf
find "$TESTDIR" -type f | sed "s|$TESTDIR|@TESTDIR@|g" | sort >> $TS_OUTPUT
$TS_HELPER_CONFIGS --etc "$ETC_DIR" --run "$RUN_DIR" --usr "$USR_DIR" \
	--project "proj" --name "example" --suffix "conf" \
	| sed "s|$TESTDIR|@TESTDIR@|g" >> $TS_OUTPUT 2>> $TS_ERRLOG
rm -f "$ETC_DIR/proj/example.conf"
rm -rf "$ETC_DIR/proj/example.conf.d"
rm -rf "$USR_DIR/proj/example.conf.d"
ts_finalize_subtest

ts_finalize
